<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2007 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: fastadmin: https://www.fastadmin.net/
// +----------------------------------------------------------------------
namespace app\admin\controller\team;

use app\admin\model\CarModel;
use app\admin\model\College;
use app\admin\model\UserModel;
use app\common\controller\Adminbase;
use app\common\model\AliPay;
use app\common\model\DouyinModel;
use app\common\model\RebateLog;
use util\Tree;


class DouYin extends Adminbase
{

    protected function initialize()
    {
        parent::initialize();
        $this->modelClass = new DouyinModel();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($page, $limit, $where, $sort, $order) = $this->buildTableParames();
            $map = [];
            if ($this->admin['roleid'] != 1) {
                $map[] = ['aid', '=', $this->admin['id']];
            }

            $list = $this->modelClass
                ->with(['staff','admin'])
                ->where(['mark' => 0])
                ->where($map)
                ->where($where)
                ->orderRaw("id desc")
                ->paginate($limit);

            $result = ["code" => 0, 'count' => $list->total(), "data" => $list->items()];
            return json($result);
        }
        $this->assign('adminId', $this->admin['id']);
        return $this->fetch();
    }


    public function check()
    {
        $id = $this->request->param('id/d', 0);
        $row = $this->modelClass->get($id);
        if (!$row) {
            $this->error('记录未找到');
        }
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post('');
            if ($params['status'] == 3 && empty($params['reason'])) {
                return $this->error('请填写不通过的理由');
            }

            try {
                $row->allowField(true)->save($params);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            $this->success("修改成功！");
        }

        $this->assign("data", $row);
        return $this->fetch();
    }


    public function edit()
    {
        $id = $this->request->param('id/d', 0);
        $date = date('Y-m-d');
        $userInfo = $this->modelClass->where('id', $id)->find();
        $data['alipay'] = (new AliPay())->where(['uid' => $userInfo['id']])->find();
        $rebate = (new RebateLog())->where(['uid' => $userInfo['id'], 'day' => $date])->find();
        $userInfo['day_money'] = number_format($rebate['money'], 2);
        $userInfo['is_auth'] = $this->is_auth[$userInfo['is_auth']];
        $data['user_info'] = $userInfo;


        $this->assign('data', $data);
        return $this->fetch();
    }


}
